<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>决策单调性优化DP 学习笔记 AND P4767 「IOI2000」 邮局 题解 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="咕咕咕">
<meta property="og:type" content="article">
<meta property="og:title" content="决策单调性优化DP 学习笔记 AND P4767 「IOI2000」 邮局 题解">
<meta property="og:url" content="http://example.com/2023/07/17/%E5%86%B3%E7%AD%96%E5%8D%95%E8%B0%83%E6%80%A7%E4%BC%98%E5%8C%96DP%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%20AND%20P4767%20%E3%80%8CIOI2000%E3%80%8D%20%E9%82%AE%E5%B1%80%20%E9%A2%98%E8%A7%A3/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="咕咕咕">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2023-07-17T14:18:57.057Z">
<meta property="article:modified_time" content="2023-07-19T11:50:20.349Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
  
<meta name="generator" content="Hexo 6.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"><span class="fa fa-bars"></span></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
        
          <a class="nav-icon" href="/atom.xml" title="RSS Feed"><span class="fa fa-rss"></span></a>
        
        <a class="nav-icon nav-search-btn" title="Search"><span class="fa fa-search"></span></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-决策单调性优化DP 学习笔记 AND P4767 「IOI2000」 邮局 题解" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2023/07/17/%E5%86%B3%E7%AD%96%E5%8D%95%E8%B0%83%E6%80%A7%E4%BC%98%E5%8C%96DP%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%20AND%20P4767%20%E3%80%8CIOI2000%E3%80%8D%20%E9%82%AE%E5%B1%80%20%E9%A2%98%E8%A7%A3/" class="article-date">
  <time class="dt-published" datetime="2023-07-17T14:18:57.057Z" itemprop="datePublished">2023-07-17</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      决策单调性优化DP 学习笔记 AND P4767 「IOI2000」 邮局 题解
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <p>咕咕咕</p>
<h2 id="0-题面"><a href="#0-题面" class="headerlink" title="0. 题面"></a>0. 题面</h2><h3 id="题目描述"><a href="#题目描述" class="headerlink" title="题目描述"></a>题目描述</h3><p>高速公路旁边有一些村庄。高速公路表示为整数轴，每个村庄的位置用单个整数坐标标识。没有两个在同样地方的村庄。两个位置之间的距离是其整数坐标差的绝对值。</p>
<p>邮局将建在一些，但不一定是所有的村庄中。为了建立邮局，应选择他们建造的位置，使每个村庄与其最近的邮局之间的距离总和最小。</p>
<p>你要编写一个程序，已知村庄的位置和邮局的数量，计算每个村庄和最近的邮局之间所有距离的最小可能的总和。</p>
<h3 id="输入格式"><a href="#输入格式" class="headerlink" title="输入格式"></a>输入格式</h3><p>第一行包含两个整数：第一个是村庄 $V$ 的数量，第二个是邮局的数量 $P$。</p>
<p>第二行包含 $V$ 个整数。这些整数是村庄的位置。</p>
<h3 id="输出格式"><a href="#输出格式" class="headerlink" title="输出格式"></a>输出格式</h3><p>第一行包含一个整数$S$，它是每个村庄与其最近的邮局之间的所有距离的总和。</p>
<h3 id="样例-1"><a href="#样例-1" class="headerlink" title="样例 #1"></a>样例 #1</h3><h4 id="样例输入-1"><a href="#样例输入-1" class="headerlink" title="样例输入 #1"></a>样例输入 #1</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">10 5 </span><br><span class="line">1 2 3 6 7 9 11 22 44 50</span><br></pre></td></tr></table></figure>
<h4 id="样例输出-1"><a href="#样例输出-1" class="headerlink" title="样例输出 #1"></a>样例输出 #1</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">9</span><br></pre></td></tr></table></figure>
<h3 id="提示"><a href="#提示" class="headerlink" title="提示"></a>提示</h3><p>对于 $40\%$ 的数据，$V \leq 300$。</p>
<p>对于 $100\%$ 的数据，$1 \leq P \leq 300$，$P \leq V  \leq 3000$，$1 \leq $ 村庄位置 $\leq 10000$。</p>
<h2 id="1-朴素算法"><a href="#1-朴素算法" class="headerlink" title="1. 朴素算法"></a>1. 朴素算法</h2><p>容易看出这是一道 DP 题。不妨令 $a<em>i$ 为位置第 $i$ 大的村庄的位置（约定$a_0=-\infty$，$a</em>{V+1}=\infty$），$f_{i,j}$ 为修建 $i$ 个邮局且最后一个邮局在 $a_j$ 处时前 $j$ 个村庄与其最近的邮局之间的所有距离的总和，$w(l,r)$ 为在 $a_l$ 和 $a_r$ 处修建邮局后在 $a_l$ 和 $a_r$ 之间的所有村庄与其最近的邮局之间的所有距离的总和。不难得出以下递推式：</p>
<script type="math/tex; mode=display">
f_{i,j}=\begin{cases}
    \min_{k=1}^{j-1} \left\{ f_{i-1,k}+w(k,j) \right\} & 2 \le i \le P \\
    w(0,j) & i = 1
\end{cases}</script><p>答案即为 <script type="math/tex">ans=\min_{j=1}^{V}\{f_{p,j}+w(j,V+1)\}</script></p>
<p>事实上，使用前缀和技术，$w$ 函数可以在 $\Theta(1)$ 的时间复杂度内计算。约定 $mid=\lfloor{\dfrac{a_l+a_r}{2}}\rfloor$，$sum_i$ 为在位置 $i$ 及其左侧所有村庄的位置值之和，$cnt_i$ 为在位置 $i$ 及其左侧所有村庄的数量，可以知道</p>
<script type="math/tex; mode=display">
\begin{aligned}
w(l,r) &= \sum_{i=l}^{r} \min\{a_i-a_l\ , a_r-a_i\ \} \\
       &= \sum_{i \ge l \  \land\  a_i \le mid}(a_i-a_l) + \sum_{i \le r \  \land\  a_i > mid}(a_r-a_i) \qquad //在\ mid\ 左侧的村庄离\ a_l 更近，在\ mid\ 右侧的村庄离\ a_r 更近 \\
       &= \sum_{i \ge l \  \land\  a_i \le mid}a_i - \sum_{i \ge l \  \land\  a_i \le mid}a_l + \sum_{i \le r \  \land\  a_i > mid}a_r - \sum_{i \le r \  \land\  a_i > mid}a_i \\
       &= (sum_{mid}-sum_{a_l-1})-a_l(cnt_{mid}-cnt_{a_l-1}) +a_r(cnt_{a_r}-cnt_{mid}) - (sum_{a_r}-sum_{mid})
\end{aligned}</script><p>于是我们得到了一个时间复杂度为 $\Theta(VP^2)$ 的算法。但是它太慢了，需要优化。</p>
<h2 id="2-决策单调性优化"><a href="#2-决策单调性优化" class="headerlink" title="2. 决策单调性优化"></a>2. 决策单调性优化</h2><p>设 $f<em>{i,j}=f</em>{i-1,p<em>{i,j}}+w(k,p</em>{i,j})$ ，我们称 $p<em>{i,j}$ 为 $f</em>{i,j}$ 的“最优决策点”。可以知道，递推式 $f$ 具有“决策单调性”，即对于每个 $i，j$，均有 $p<em>{i,j} \le p</em>{i,j+1}$。证明如下：</p>
<script type="math/tex; mode=display">
先放着，以后再证\\
反正大概就是证明“四边形不等式”，即 w(a,c)+w(b,d)\le w(a,d)+w(b,c)\ (a\le b\le c\le d) ，\\
进而证明 f具有单调性，即f_{i,j}\le f_{i,j+1}\\
（其实从f的意义上也能看出，因为两个邮局离得越远距离和就越大）</script><p>因此可以发现，如果求得 $p_i$，则可以知道 $p_j\le p_i(j<i)$，$p_j\ge p_i(j>i)$ ，这样我们枚举法求 $p$ 时就可以排除许多无用状态。因此可以想到一个分治算法，代码如下：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="type">void</span> <span class="title">dfs</span><span class="params">(<span class="type">int</span> l,<span class="type">int</span> r,<span class="type">int</span> ll,<span class="type">int</span> rr)</span></span>&#123;</span><br><span class="line">    <span class="comment">//l,r: f下标范围</span></span><br><span class="line">    <span class="comment">//ll,rr: p可能的范围</span></span><br><span class="line">    <span class="keyword">if</span>(l&gt;r)<span class="keyword">return</span>;</span><br><span class="line">    <span class="type">int</span> mid=(l+r)/<span class="number">2</span>,p=<span class="number">0</span>;</span><br><span class="line">    f[mid]=INF;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=ll;i&lt;mid;i++)&#123;</span><br><span class="line">        <span class="keyword">if</span>(f[mid]&gt;g[i]+<span class="built_in">w</span>(a[i],a[mid]))&#123;</span><br><span class="line">            f[mid]=g[i]+<span class="built_in">w</span>(a[i],a[mid]);</span><br><span class="line">            p=i;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;<span class="comment">//枚举求</span></span><br><span class="line">    <span class="built_in">dfs</span>(l,mid<span class="number">-1</span>,ll,p);</span><br><span class="line">    <span class="built_in">dfs</span>(mid+<span class="number">1</span>,r,p,rr);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>不难发现这个算法的时间复杂度是 $\Theta(VP\log V)$ ，足以通过此题。完整代码如下：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;iostream&gt;</span><br><span class="line">#include &lt;algorithm&gt;</span><br><span class="line">using namespace std;</span><br><span class="line">const int MAXP=300,MAXV=3000,MAXX=10000,INF=0x3f3f3f3f;</span><br><span class="line">int v,p,a[MAXV+5],cnt[MAXX+5],sum[MAXX+5],pre[MAXX+5],suf[MAXX+5],R,f[MAXV+5],g[MAXV+5];</span><br><span class="line">int w(int l,int r)&#123;</span><br><span class="line">    int mid1=(l+r)/2;</span><br><span class="line">    return sum[mid1]-sum[l-1]-l*(cnt[mid1]-cnt[l-1])+r*(cnt[r]-cnt[mid1])-(sum[r]-sum[mid1]);</span><br><span class="line">&#125;</span><br><span class="line">void dfs(int l,int r,int ll,int rr)&#123;</span><br><span class="line">    if(l&gt;r)return;</span><br><span class="line">    int mid=(l+r)/2,k=0;</span><br><span class="line">    f[mid]=INF;</span><br><span class="line">    for(int i=ll;i&lt;mid;i++)&#123;</span><br><span class="line">        if(f[mid]&gt;g[i]+w(a[i],a[mid]))&#123;</span><br><span class="line">            f[mid]=g[i]+w(a[i],a[mid]);</span><br><span class="line">            k=i;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    dfs(l,mid-1,ll,k);</span><br><span class="line">    dfs(mid+1,r,k,rr);</span><br><span class="line">&#125;</span><br><span class="line">int main()&#123;</span><br><span class="line">    ios::sync_with_stdio(false);</span><br><span class="line">    cin&gt;&gt;v&gt;&gt;p;</span><br><span class="line">    for(int i=1;i&lt;=v;i++)&#123;</span><br><span class="line">        cin&gt;&gt;a[i];</span><br><span class="line">        cnt[a[i]]++;</span><br><span class="line">        sum[a[i]]+=a[i];</span><br><span class="line">    &#125;</span><br><span class="line">    sort(a+1,a+1+v);</span><br><span class="line">    R=a[v];</span><br><span class="line">    for(int i=1;i&lt;=R;i++)&#123;</span><br><span class="line">        cnt[i]+=cnt[i-1];</span><br><span class="line">        sum[i]+=sum[i-1];</span><br><span class="line">    &#125;</span><br><span class="line">    for(int i=1;i&lt;=R;i++)&#123;</span><br><span class="line">        pre[i]=i*cnt[i]-sum[i];</span><br><span class="line">        suf[i]=sum[R]-sum[i-1]-i*(cnt[R]-cnt[i-1]);</span><br><span class="line">    &#125;</span><br><span class="line">    for(int i=1;i&lt;=v;i++)&#123;</span><br><span class="line">        f[i]=pre[a[i]];</span><br><span class="line">    &#125;</span><br><span class="line">    int ans=INF;</span><br><span class="line">    for(int i=2;i&lt;=p;i++)&#123;</span><br><span class="line">        swap(f,g);</span><br><span class="line">        dfs(1,v,1,v);</span><br><span class="line">    &#125;</span><br><span class="line">    for(int i=1;i&lt;=v;i++)&#123;</span><br><span class="line">        ans=min(ans,f[i]+suf[a[i]]);</span><br><span class="line">    &#125;</span><br><span class="line">    cout&lt;&lt;ans&lt;&lt;endl;</span><br><span class="line">    return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2023/07/17/%E5%86%B3%E7%AD%96%E5%8D%95%E8%B0%83%E6%80%A7%E4%BC%98%E5%8C%96DP%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%20AND%20P4767%20%E3%80%8CIOI2000%E3%80%8D%20%E9%82%AE%E5%B1%80%20%E9%A2%98%E8%A7%A3/" data-id="clk9o8ln400098wi5a5y9dh2y" data-title="决策单调性优化DP 学习笔记 AND P4767 「IOI2000」 邮局 题解" class="article-share-link"><span class="fa fa-share">Share</span></a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
  
    <a href="/2022/10/30/CSP-S%202022%20%E6%B8%B8%E8%AE%B0/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">CSP-S 2022 游记</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2023/07/">July 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/10/">October 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/05/">May 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/02/">February 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/01/">January 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/08/">August 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/07/">July 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/06/">June 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/08/">August 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2023/07/17/%E5%86%B3%E7%AD%96%E5%8D%95%E8%B0%83%E6%80%A7%E4%BC%98%E5%8C%96DP%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%20AND%20P4767%20%E3%80%8CIOI2000%E3%80%8D%20%E9%82%AE%E5%B1%80%20%E9%A2%98%E8%A7%A3/">决策单调性优化DP 学习笔记 AND P4767 「IOI2000」 邮局 题解</a>
          </li>
        
          <li>
            <a href="/2022/10/30/CSP-S%202022%20%E6%B8%B8%E8%AE%B0/">CSP-S 2022 游记</a>
          </li>
        
          <li>
            <a href="/2022/05/29/%E6%96%9C%E7%8E%87%E4%BC%98%E5%8C%96%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">斜率优化动态规划 学习笔记</a>
          </li>
        
          <li>
            <a href="/2022/05/22/PKUSC%E4%B8%80%E5%8F%A5%E8%AF%9D%E6%B8%B8%E8%AE%B0/">PKUSC一句话游记</a>
          </li>
        
          <li>
            <a href="/2022/05/18/%E7%9C%81%E9%80%89%E6%B8%B8%E8%AE%B0%EF%BC%9F%E7%9C%81%E9%80%89%E6%B8%B8%E5%AF%84%EF%BC%81/">省选游记？省选游寄！</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2023 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.6.4.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>